﻿using Microsoft.Practices.EnterpriseLibrary.Data;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TN_CMS.Domain.Abstract;
using TN_CMS.Domain.Entities;

namespace TN_CMS.Domain.Concrete
{
    public class EFSysRoleRepository : BaseRepository, ISysRoleRepository
    {
        private EFDbContext _context;

        public EFSysRoleRepository(EFDbContext context = null)
        {
            this._context = (context != null) ? context : new EFDbContext();
        }

        public IQueryable<SysRole> SysRoles
        {
            get { return this._context.SysRoles; }
        }

        public List<SysRole> GetList(int pageIndex, int pageSize, out int total, string orderClause, string whereClause)
        {
            var map = MapBuilder<SysRole>
                .MapAllProperties()
                .Build();
            var parameters = new DbParameter[] { 
                    this._db.CreateParameter("PageIndex", pageIndex, DbType.Int32),
                    this._db.CreateParameter("PageSize", pageSize, DbType.Int32),
                    this._db.CreateParameter("TotalCounts", 0, DbType.Int32, ParameterDirection.Output),
                    this._db.CreateParameter("OrderClause", orderClause, DbType.String),
                    this._db.CreateParameter("WhereClause", whereClause, DbType.String)
                };
            var lst = this._db.Execute("SP_SysRole_GetList", map, parameters).ToList();
            total = (int)parameters[2].Value;

            return lst;
        }

        public SysRole GetDetail(int id, bool getInactive = true)
        {
            return SysRoles.FirstOrDefault(a => a.Id == id && (a.IsActive == true || a.IsActive == !getInactive));
        }

        public int Save(SysRole role)
        {
            bool needToSave = false;

            if (role.Id == -1)
            {
                _context.SysRoles.Add(role);
                needToSave = true;
            }
            else
            {
                SysRole dbEntry = GetDetail(role.Id);
                if (dbEntry != null)
                {
                    dbEntry.Name = role.Name;
                    dbEntry.Key = role.Key;
                    dbEntry.Description = role.Description;
                    dbEntry.IsSystemRole = role.IsSystemRole;
                    dbEntry.IsActive = role.IsActive;
                    needToSave = true;
                }
            }

            if (needToSave)
            {
                return _context.SaveChanges();
            }
            else
            {
                return 0;
            }
        }

        public int Delete(int[] ids)
        {
            bool needSaveChange = false;

            foreach (var id in ids)
            {
                SysRole dbEntry = GetDetail(id);
                if (dbEntry != null)
                {
                    _context.SysRoles.Remove(dbEntry);
                    needSaveChange = true;
                }
            }

            if (needSaveChange)
            {
                return _context.SaveChanges();
            }
            else
            {
                return 0;
            }
        }
    }
}
